package cn.ictgu.parse.crawler;

import cn.ictgu.config.OtherParseConfig;
import cn.ictgu.dao.model.Juji;
import cn.ictgu.dto.VideoInfo;
import cn.ictgu.parse.VideoParse;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 优酷视频
 * Created by Silence on 2017/1/7.
 */
@Log4j
public class Youku extends VideoParse {
  private final static String YOUKU = "优酷视频";

  private final static String VIDEO_INFO_API = "http://play-ali.youku.com/play/get.json?vid=%s&ct=12";
  private static final String YOUKU_TV_VALUE = "http://v.youku.com/v_show/id_%s.html";

  @Override
  public VideoInfo parseVideoFromUrl(String videoUrl) {
    VideoInfo videoInfo = createVideoInfoWithCopyRight(YOUKU, videoUrl, OtherParseConfig.OFFICIAL_NAME, OtherParseConfig.OFFICIAL_WEBSITE);
    String title = crawlerTitle(videoUrl);
    videoInfo.setTitle(title);
    String playUrl = commenParse(videoUrl);
    videoInfo.setPlayUrl(playUrl);
    videoInfo.setType("m3u8");
    return videoInfo;
  }

  @Override
  public List<Juji> parseJujiFromUrl(String videoUrl) {
    List<Juji> jujis = new ArrayList<>();
    String vid = this.matchVid(videoUrl);
    Document document = this.getDocumentWithPCUA(String.format(YOUKU_TV_VALUE, vid));
    Elements elements = document.select("div.items div[name=tvlist]");
    if (elements.size() > 0) {
      for (Element element : elements) {
        Juji juji = new Juji();
        int index = Integer.valueOf(element.select("a.sn span").text().trim());
        if (index < 10) {
          juji.setIndex("0" + index);
        } else {
          juji.setIndex("" + index);
        }
        String value = "http:" + element.select("a.sn").attr("href");
        juji.setValue(value);
        jujis.add(juji);
      }
    } else {
      elements = document.select("div.lists div.items li.item");
      for (Element element : elements) {
        Juji juji = new Juji();
        int index = Integer.valueOf(element.attr("seq").trim());
        if (index < 10) {
          juji.setIndex("0" + index);
        } else {
          juji.setIndex("" + index);
        }
        String value = "http:" + element.select("a.A").attr("href");
        juji.setValue(value);
        jujis.add(juji);
      }
    }
    Collections.reverse(jujis);
    return jujis;
  }

  private String matchVid(String url){
    Matcher matcher = Pattern.compile("id_(.*?)(_|\\.html)").matcher(url);
    if (matcher.find()){
      return matcher.group(1);
    }
    return "";
  }

  private String crawlerTitle(String videoUrl){
    String vid = matchVid(videoUrl);
    String api = String.format(VIDEO_INFO_API, vid);
    JSONObject data = JSONObject.parseObject(getDocument(api).text());
    return data.getJSONObject("data").getJSONObject("video").getString("title");
  }

}
